package com.squirrels.server.work;

import com.squirrels.server.common.constant.Constant;
import com.squirrels.server.common.constant.SystemConstant;
import com.squirrels.server.message.protobuf.Message;
import com.squirrels.server.utils.JWTUtils;
import dev.morphia.Datastore;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.socket.DatagramPacket;
import org.springframework.util.StringUtils;

import java.net.InetSocketAddress;
import java.nio.charset.StandardCharsets;

/**
 * 创建者: FlyingSquirrels
 * 创建时间: 2024/7/30 10:03
 * 描述: 公共处理器
 */
public abstract class ServerWorker implements Cloneable{

    // mongoDB数据库操控类，启动后初始化
    protected Datastore datastore;

    // 通道连接
    protected ChannelHandlerContext ctx;
    // 通道数据
    protected Message.Entity entity;
    // 发送人
    protected InetSocketAddress sender;

    // 初始化datastore
    public void setDatastore(Datastore datastore) {
        this.datastore = datastore;
    }

    // 执行操作
    public void exec(ChannelHandlerContext ctx, InetSocketAddress sender, Message.Entity entity){
        this.ctx = ctx;
        this.entity = entity;
        this.sender = sender;
        String operationBase = entity.getOperationBase();
        // 根据请求头部执行操作
        Message.Entity responseData = doWorker(operationBase);
        // 过滤不需要返回值的请求
        if(null == responseData){
            return;
        }
        DatagramPacket datagramPacket = new DatagramPacket(
                ctx.alloc().buffer().writeBytes(responseData.toByteArray()),
                sender
        );
        // 响应数据
        ctx.writeAndFlush(datagramPacket);
    };

    // 子类实现具体操作方法，返回数据对象
    public abstract Message.Entity doWorker(String operationBase);

    //公共错误信息
    protected Message.Entity errorMsg(Constant constant,Message.Entity entity){
        Message.Data data = Message.Data.newBuilder().setInfo(constant.getInfo()).build();
        return Message.Entity.newBuilder()
                .setOperation(entity.getOperation())
                .setOperationBase(entity.getOperationBase())
                .setCode((int)constant.getValue())
                .setDataType(Message.Entity.EntityType.DataType)
                .setData(data)
                .build();
    }

    //公共信息
    protected Message.Entity msg(Constant constant,Message.Entity entity){
        return errorMsg(constant,entity);
    }


    // 检查是否登录为登录响应401
    protected Message.Entity checkLogin(){
        String token = entity.getToken();
        if(null != token && !("".equals(token)) && JWTUtils.jwtUtils.checkToken(token)){
            return null;
        }
        Message.Entity notFund = Message.Entity.newBuilder()
                .setCode((Integer) SystemConstant.NOT_LOGIN.getValue())
                .setDataType(Message.Entity.EntityType.DataType)
                .setData(Message.Data.newBuilder().setInfo(SystemConstant.NOT_LOGIN.getInfo()))
                .build();
        return notFund;
    }

    // 克隆工作类
    @Override
    public ServerWorker clone() throws CloneNotSupportedException {
        return (ServerWorker) super.clone();
    }

}
